using System;
using System.Collections.Generic;
using System.Text;

namespace VirtualOps.Base
{
  public abstract class VmServer
  {

    //TODO: consider switch to using System.Uri
    private string _uri;
    public string Uri
    {
      get { return _uri; }
      set { _uri = value; }
    }
    abstract public bool Connected { get; }
    
    abstract public IVirtualMachine[] GetAllVms();

    public static T CreateInstance<T>(string uri) where T : VmServer, new()
    {

      if (ShouldCreateNewForUri(uri))
      {
        T instance = new T();
        instance.Uri = uri;
        _this = instance;
        return instance;
      }
      else
      {
        return _this as T;
      }
    }

    //TODO: consider moving lazy init logic to child classes
    private static bool ShouldCreateNewForUri(string uri)
    {
      if (_this == null) return true;

      //System.Uri currentUri = new System.Uri(_this._uri);
      //System.Uri newUri = new System.Uri(uri);
      //return newUri != currentUri;
      int compareRes = String.Compare(uri, _this._uri, true);
      return compareRes != 0;
    }

    /** Reference to last used/default/current server */
    static public VmServer Current 
    {
      get { return _this; }
    }
    static private VmServer _this;  
  }
}
